package com.hskn.hss.module.bigscreen.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.bigscreen.entity.vo.Guideboard;
import com.hskn.hss.module.bigscreen.entity.vo.JxqsKaoQin;
import com.hskn.hss.module.bigscreen.entity.vo.jxqsscene.JxqsWorkPlan;
import com.hskn.hss.module.bigscreen.service.IBigScreenService;
import com.hskn.hss.module.bigscreen.to.WorkPlanTo;
import com.hskn.hss.module.bigscreen.vo.ExpListTO;
import com.hskn.hss.module.cases.entity.Cases;
import com.hskn.hss.module.cases.service.ICasesService;
import com.hskn.hss.module.dataBigscreen.vo.AbnormalTypeVo;
import com.hskn.hss.module.rptCarWork.entity.RptCarWork;
import com.hskn.hss.module.rptCarWork.mapper.RptCarWorkMapper;
import com.hskn.hss.module.tlcar.service.ITlCarService;
import com.hskn.hss.module.tlemployee.service.ITlEmployeeService;
import com.hskn.hss.module.tlfacilities.service.ITlFacilitiesService;
import com.hskn.hss.module.tlthreshold.mapper.TlThresholdMapper;
import com.hskn.hss.utils.AttributeUtils;
import com.hskn.hss.utils.MathUtils;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-20
 */
@Api(tags = {"-bigscreen"}, description = "一张网大屏幕")
@Slf4j
@RestController
@RequestMapping("/hss/bigscreen")
public class BigScreenController {

    @Autowired
    private ITlCarService tlCarService;

    @Autowired
    private ITlEmployeeService tlEmployeeService;

    @Autowired
    private ICasesService casesService;

    @Autowired
    private ITlFacilitiesService tlFacilitiesService;

    @Autowired
    IBigScreenService bigScreenService;

    @Resource
    RptCarWorkMapper rptCarWorkMapper;

    @ApiOperation(value = "车辆异常类型接口", notes = "查看车辆异常类型")
    @RequestMapping(value = "expTypeCar", method = RequestMethod.POST)
    public AjaxResult<Object> expTypeCar() {
        try {
            List<AbnormalTypeVo> expTypeCars = bigScreenService.expTypeCars();
            return AjaxResult.success(expTypeCars);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "人员异常类型接口", notes = "查看人员异常类型")
    @RequestMapping(value = "expTypePerson", method = RequestMethod.POST)
    public AjaxResult<Object> expTypePerson() {
        try {
            List<AbnormalTypeVo> expTypePeople = bigScreenService.expTypePerson();
            return AjaxResult.success(expTypePeople);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "设施异常类型接口", notes = "查看设施异常类型")
    @RequestMapping(value = "expTypeFacilities", method = RequestMethod.POST)
    public AjaxResult<Object> expTypeFacilities() {
        try {
            List<AbnormalTypeVo> expTypeFacilities = bigScreenService.expTypeFacilities();
            return AjaxResult.success(expTypeFacilities);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "车辆异常列表", notes = "查看车辆异常列表")
    @PostMapping(value = "expCarList")
    public AjaxResult<Object> expCarList(@Validated @RequestBody ExpListTO expListTO) {
        try {
            //已改 不查视图
            JSONObject json = bigScreenService.expCarList(expListTO);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "人员异常列表", notes = "查看人员异常列表")
    @RequestMapping(value = "expPersonList", method = RequestMethod.POST)
    public AjaxResult<Object> expPersonList(@Validated @RequestBody ExpListTO expListTO) {
        try {
            //已改 不查视图
            JSONObject json = bigScreenService.expPersonList(expListTO);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "设施异常列表", notes = "查看设施异常列表")
    @RequestMapping(value = "expFacilitiesList", method = RequestMethod.POST)
    public AjaxResult<Object> expFacilitiesList(@Validated @RequestBody ExpListTO expListTO) {
        try {
            //已改 不查视图
            JSONObject json = bigScreenService.expFacilitiesList(expListTO);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "当日人车异常", notes = "查看当日人车异常")
    @RequestMapping(value = "todayException", method = RequestMethod.POST)
    public AjaxResult<Object> todayException() {
        try {
            JSONObject json = bigScreenService.todayException();
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "carOnGuard", method = RequestMethod.POST)
    public AjaxResult<Object> carOnGuard(HttpServletRequest request) {
        try {
            return AjaxResult.success(bigScreenService.carOnGuard());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "employeeOnGuard", method = RequestMethod.POST)
    public AjaxResult<Object> employeeOnGuard(HttpServletRequest request) {
        try {
            return AjaxResult.success(bigScreenService.employeeOnGuard());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "workSupervise", method = RequestMethod.POST)
    public AjaxResult<Object> workSupervise(HttpServletRequest request) {
        try {
            JSONObject json = new JSONObject();
            String now = DateUtil.formatDate(new Date());
            // 作业类型：2机械清扫；3垃圾清运；4垃圾转运
            Map ljqyMap = new HashMap<>();
            ljqyMap.put("workType", 3);
            ljqyMap.put("workDate", now);
            Map<String, Integer> ljqyResult = rptCarWorkMapper.getCarCompleteStat(ljqyMap);
            BigDecimal ljqyCompleteNum = MathUtils.getBigDecimal(ljqyResult.get("completeNum"));
            BigDecimal ljqyNotCompleteNum = MathUtils.getBigDecimal(ljqyResult.get("notCompleteNum"));

            BigDecimal ljqyNumber = ljqyCompleteNum.add(ljqyNotCompleteNum);
            BigDecimal ljqy = BigDecimal.ZERO;
            if (BigDecimal.ZERO != ljqyNumber) {
                ljqy = ljqyCompleteNum == BigDecimal.ZERO ? BigDecimal.ZERO : (ljqyCompleteNum.multiply(new BigDecimal(100))).divide(ljqyNumber, 4, BigDecimal.ROUND_HALF_UP).setScale(0, BigDecimal.ROUND_UP);
            }
            json.put("ljqy", ljqy);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    @RequestMapping(value = "workResource", method = RequestMethod.POST)
    public AjaxResult<Object> workResource(HttpServletRequest request) {
        try {
            return AjaxResult.success(bigScreenService.workResource());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "guideBoards", method = RequestMethod.POST)
    public AjaxResult<Object> guideBoards(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List<Guideboard> guideBoards = bigScreenService.guideBoards(params);
            return AjaxResult.success(guideBoards);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "personMonitor", method = RequestMethod.POST)
    public AjaxResult<Object> personMonitor(HttpServletRequest request) {
        try {
            return AjaxResult.success(bigScreenService.personMonitor());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "mechanicalCarMonitor", method = RequestMethod.POST)
    public AjaxResult<Object> mechanicalCarMonitor() {
        try {
            return AjaxResult.success(bigScreenService.mechanicalCarMonitor());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "pickupCarMonitor", method = RequestMethod.POST)
    public AjaxResult<Object> pickupCarMonitor() {
        try {
            return AjaxResult.success(bigScreenService.pickupCarMonitor());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "toDayAttendance", method = RequestMethod.POST)
    public AjaxResult<Object> toDayAttendance(HttpServletRequest request) {
        try {
            return AjaxResult.success(bigScreenService.toDayAttendance());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "todayOutcome", method = RequestMethod.POST)
    public AjaxResult<Object> todayOutcome(HttpServletRequest request) {
        try {
            String day = DateUtil.formatDate(new Date());
            String startTime = AttributeUtils.timeCompletion(day, 's');
            String endTime = AttributeUtils.timeCompletion(day, 'e');

            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            QueryWrapper<Cases> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().between(Cases::getCreateTime, startTime, endTime);
            int wtsb = casesService.count(queryWrapper);
            json.put("zyqs", 0);
            json.put("wcld", 0);
            json.put("wtsb", wtsb);
            json.put("zysc", 0);
            json.put("hbzr", 0);
            json.put("zrzysc", 0);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    @RequestMapping(value = "mapElementList", method = RequestMethod.POST)
    public AjaxResult<Object> mapElementList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            String depid = params.getString("depid");
            JSONObject json = new JSONObject();
            json.put("facilities", tlFacilitiesService.list());
            QueryWrapper<Cases> casesQueryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(depid)) {
                casesQueryWrapper.lambda().eq(Cases::getDepid, depid);
            }
            Integer[] status = new Integer[]{1, 2, 3};
            casesQueryWrapper.lambda().in(Cases::getStatus, Arrays.asList(status));
            json.put("cases", casesService.list(casesQueryWrapper));
            json.put("car", tlCarService.list());
            json.put("person", tlEmployeeService.list());
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "ssjkTodayExp", method = RequestMethod.POST)
    public AjaxResult<Object> ssjkTodayExp(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            json.put("aqcb", 0);
            json.put("lhqcb", 0);
            json.put("ywcb", 0);
            json.put("qt", 0);
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "ljdw", method = RequestMethod.POST)
    public AjaxResult<Object> ljdw(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = new JSONObject();
            return AjaxResult.success(json);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "jxzyWorkSceneList", method = RequestMethod.POST)
    public AjaxResult<Object> jxzyWorkSceneList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List<JxqsKaoQin> jxqsKaoQinList = bigScreenService.jxzyWorkSceneList(params);
            return AjaxResult.success(jxqsKaoQinList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "jxzyWorkDetail", method = RequestMethod.POST)
    public AjaxResult<Object> jxzyWorkDetail(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List<RptCarWork> rptCarWorkList = bigScreenService.jxzyWorkDetail(params);
            return AjaxResult.success(rptCarWorkList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "getJxqsWorkByCar", method = RequestMethod.POST)
    public AjaxResult<Object> getJxqsWorkByCar(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List<JxqsWorkPlan> jxqsWorkPlans = bigScreenService.getJxqsWorkByCar(params);
            return AjaxResult.success(jxqsWorkPlans);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "jxzyWorkScene", method = RequestMethod.POST)
    public AjaxResult<Object> jxzyWorkScene(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject scene = bigScreenService.jxzyWorkScene(params);
            return AjaxResult.success(scene);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "ljqyWorkScene", method = RequestMethod.POST)
    public AjaxResult<Object> ljqyWorkScene(HttpServletRequest request) {
        try {
            JSONObject scene = bigScreenService.ljqyWorkScene();
            return AjaxResult.success(scene);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    @RequestMapping(value = "treeData", method = RequestMethod.POST)
    public AjaxResult<Object> treeData(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List list = bigScreenService.treeData(params);
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "allWorkByApp", method = RequestMethod.POST)
    public AjaxResult<Object> allWorkByApp(HttpServletRequest request) {
        try {
            return AjaxResult.success(bigScreenService.allWorkByApp());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "emergencyByApp", method = RequestMethod.POST)
    public AjaxResult<Object> emergencyByApp(HttpServletRequest request) {
        try {
            return AjaxResult.success(bigScreenService.emergencyByApp());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "getPlanList", method = RequestMethod.POST)
    public AjaxResult<Object> getPlanList(@RequestBody @Validated WorkPlanTo workPlanTo) {
        try {
            return AjaxResult.success(bigScreenService.getPlanList(workPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
}

